package controllers;

import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JTextField;

import models.CustomTable;
import models.CustomTableModel;
import models.CustomerSearchModel;
import system.View;
import dialogs.SearchItemDialog;

public class CustomerSearchController extends AbstractController 
{
	private CustomerSearchModel custModel = new CustomerSearchModel();
	public CustomerSearchController(View v) 
	{
		super(v);
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		String actionCommand = e.getActionCommand();
		if (actionCommand.equals("Search"))
			showDialogWindow(new SearchItemDialog(view, actionCommand,
					"Search", new String[] { "Title", "Category", "Lead Singer" }, this));
	}

	public int performSearch(JTextField title, JTextField category, JTextField leadSinger)
	{
		String sTitle, sCategory, sLeadSinger;
		Vector<String> v = new Vector<String>();
		int typesOfInput=0;
		if(title.getText().length()==0 && category.getText().length()==0 && leadSinger.getText().length()==0)
			return VALIDATIONERROR;
		if(title.getText().length()!=0)
		{
			sTitle=title.getText();
			v.add(sTitle);
			typesOfInput=typesOfInput+100;
		}
		
		if(category.getText().length()!=0)
		{
			sCategory=category.getText();
			v.add(sCategory);
			typesOfInput+=10;
		}
		if(leadSinger.getText().length()!=0)
		{
			sLeadSinger=leadSinger.getText();
			v.add(sLeadSinger);
			typesOfInput=typesOfInput+001;
		}
		ResultSet rs;
		if( (rs = custModel.querySearch(v,typesOfInput)) != null)
		{
			CustomTableModel model = new CustomTableModel(custModel.getConnection(), rs);
			CustomTable data = new CustomTable(model);
		
			model.addExceptionListener(this);
			data.addExceptionListener(this);
			    
			view.addTable(data);
			return operationSuccess();
		}
		else
			return operationFailure();
	}
	
	public int validateInsert(Vector<String> s) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}

	@Override
	public int validateDelete(Vector<String> s) {
		// TODO Auto-generated method stub
		return 0;
	}

}
